package com.small.nacos.naming.consistency.ephemeral.distro.combined;

import com.small.nacos.core.distributed.distro.component.DistroFailedTaskHandler;
import com.small.nacos.core.distributed.distro.entity.DistroKey;
import com.small.nacos.core.distributed.distro.task.DistroTaskEngineHolder;
import com.small.nacos.core.distributed.distro.task.delay.DistroDelayTask;
import com.small.nacos.naming.consistency.DataOperation;
import com.small.nacos.naming.misc.GlobalConfig;

/**
 * @Author zhoujin
 * @Date 2022/11/13 18:19
 */
public class DistroHttpCombinedKeyTaskFailedHandler implements DistroFailedTaskHandler {

    private final GlobalConfig globalConfig;

    private final DistroTaskEngineHolder distroTaskEngineHolder;

    public DistroHttpCombinedKeyTaskFailedHandler(GlobalConfig globalConfig,
                                                  DistroTaskEngineHolder distroTaskEngineHolder) {
        this.globalConfig = globalConfig;
        this.distroTaskEngineHolder = distroTaskEngineHolder;
    }

    @Override
    public void retry(DistroKey distroKey, DataOperation action) {
        DistroKey newKey = new DistroKey(distroKey.getResourceKey(), distroKey.getResourceType(), distroKey.getTargetServer());
        DistroDelayTask newTask = new DistroDelayTask(newKey, action, globalConfig.getSyncRetryDelay());
        distroTaskEngineHolder.getDelayTaskExecuteEngine().addTask(newKey, newTask);
    }
}
